#include <pe.h>

IMAGE_SECTION_HEADER *pe_find_section(const void *module, const char *section) {
    const char *m = (const char*)module;

    IMAGE_DOS_HEADER* dos_header = (IMAGE_DOS_HEADER*)module;
    IMAGE_NT_HEADERS64* nt_headers = (IMAGE_NT_HEADERS64*)(m + dos_header->e_lfanew);

    WORD section_count = nt_headers->FileHeader.NumberOfSections;
    IMAGE_SECTION_HEADER* section_header = (IMAGE_SECTION_HEADER*)(nt_headers + 1);

    for (WORD i = 0; i < section_count; i++) {
        if (strncmp((char*)section_header->Name, section, 8) == 0) {
            return section_header;
        }

        section_header++;
    }

    return NULL;
}

void *pe_find_entry_point(HMODULE module) {
    char *m = (char*)module;

    IMAGE_DOS_HEADER* dos_header = (IMAGE_DOS_HEADER*)module;
    IMAGE_NT_HEADERS64* nt_headers = (IMAGE_NT_HEADERS64*)(m + dos_header->e_lfanew);

    return m + nt_headers->OptionalHeader.AddressOfEntryPoint;
}
